<?php

namespace app\common\logic;

use app\common\model\TeamActivity;
use app\common\model\Goods;
use think\Db;

/**
 * 拼团商品逻辑定义
 * Class CatsLogic
 * @package common\Logic
 */
class TeamActivityLogic extends Prom
{
    protected $TeamActivity;//拼团活动模型
    protected $goods;//商品模型

    public function __construct($goods)
    {
        parent::__construct();
        $this->goods = $goods;

        //活动商品没有规格，活动和规格是一对多
        $this->TeamActivity = TeamActivity::get($goods['prom_id'], '', true);

        if ($this->TeamActivity) {
            //每次初始化都检测活动是否失效，如果失效就更新活动和商品恢复成普通商品
            if ($this->checkActivityIsEnd() && $this->TeamActivity['is_end'] == 0) {

                Db::name('goods')->where("id", $this->goods['id'])->update(['prom_type' => 0, 'prom_id' => 0]);

                $this->TeamActivity->is_end = 1;
                $this->TeamActivity->save();
                unset($this->goods);
                $this->goods = Goods::get($goods['id'], '', true);
            }
        }
    }

    public function getPromModel()
    {
        return $this->TeamActivity;
    }

    /**
     * 活动是否正在进行
     * @return bool
     */
    public function checkActivityIsAble()
    {
        if (empty($this->TeamActivity)) {
            return false;
        }
        if (time() > $this->TeamActivity['start_time'] && time() < $this->TeamActivity['end_time'] && $this->TeamActivity['status'] == 1 && $this->TeamActivity['is_end'] == 0) {
            return true;
        }
        return false;
    }

    /**
     * 活动是否结束
     * @return bool
     */
    public function checkActivityIsEnd()
    {
        if (empty($this->TeamActivity)) {
            return true;
        }
        if (time() > $this->TeamActivity['end_time']) {
            return true;
        }
        return false;
    }

    /**
     * 获取商品原始数据
     * @return Goods
     */
    public function getGoodsInfo()
    {
        return $this->goods;
    }

    /**
     * 该活动是否已经失效
     */
    public function IsAble()
    {
        if (empty($this->TeamActivity)) {
            return false;
        }
        if (time() > $this->TeamActivity['end_time']) {
            return false;
        }
        if ($this->TeamActivity['status'] != 1) {
            return false;
        }
        if ($this->TeamActivity['is_end'] == 1) {
            return false;
        }
        return true;
    }

    /**
     * 获取商品转换活动商品的数据
     * @return static
     */
    public function getActivityGoodsInfo()
    {
        $activityGoods = $this->goods;
        $activityGoods['activity_title'] = $this->TeamActivity['name'];
        $activityGoods['market_price'] = $this->goods['market_price'];
        $activityGoods['old_shop_price'] = $this->goods['shop_price'];
        $activityGoods['start_time'] = $this->TeamActivity['start_time'];
        $activityGoods['end_time'] = $this->TeamActivity['end_time'];
        $activityGoods['shop_price'] = $this->TeamActivity['team_price'];
        $activityGoods['needer'] = $this->TeamActivity['needer'];
        $activityGoods['sales_sum'] = $this->TeamActivity['sales_sum'];
        $activityGoods['needer'] = $this->TeamActivity['needer'];
        $activityGoods['item_id'] = $this->TeamActivity['item_id'];
        $activityGoods['store_count'] = $this->goods['store_count'];

        $teamFoundLogic = new \app\common\logic\TeamFoundLogic();
        $teamFoundIds = Db::name('team_found')->where(['team_id' => $this->goods['prom_id']])->column('id');
        $teamFoundLogic->setTeam($this->TeamActivity);
        $teamList = [];
        if (!empty($teamFoundIds)) {
            foreach ($teamFoundIds as $k => $v) {
                $teamFound = \app\common\model\TeamFound::get(function ($query) use ($v) {
                    $query->where('found_end_time', '>', time());
                    $query->where('id', $v)->limit(2);
                    $query->where('status',1);
                });

                if ($teamFound['need'] - $teamFound['join'] > 0) {
                    $teamFound['haicha'] = ($teamFound['need'] - $teamFound['join']);
                    $teamFound['daojishi'] = ($teamFound['found_end_time'] - time());
                    array_push($teamList, $teamFound);
                }

            }

            $activityGoods['teamList'] = $teamList;

        }


        return $activityGoods;
    }
}